// pages/indexAll/yiqiStep2/yiqiStep2.js
const app = getApp()
var self;
var id, loginid;
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    xiangmuArr: [{
      "id": 0,
      "name": "请选择"
    }],
    xiangmuIndex: 0,
    yanjiuArr: [],
    yanjiuIndex:0,
    ketiArr: [],
    ketiIndex: 0,
    ceshiArr: [],
    ceshiIndex: 0,
    num: 1,
    startdate: "2019-07-11",
    enddate: "2019-07-11",
    starttime: "00:00",
    endtime: "00:30",
    history_list: [],
    tel:""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    self = this;
    var tmpdata = JSON.parse(options.tmpdata);
    id = options.id;
    loginid = options.loginid;
    self.setData({
      startdate: tmpdata.mindate,
      enddate: tmpdata.maxdate,
      starttime: tmpdata.mintime,
      endtime: tmpdata.maxtime,
    })
    this.getAllData();
  },
  //获取测试项目和付款课题
  getAllData: function() {
    var ShiJian = this.data.startdate + " " + this.data.starttime;
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=GetS2BangDingList",
      data: {
        YiQiID: id,
        LoginID: loginid,
        ShiJian: ShiJian,
      },
      method: 'GET',
      success: function(res) {
        console.log(res);
        if (res.data.state == 1) {
          var ceshiArr = [{
            id: 0,
            name: '请选择',
            sim_name: '请选择',
            is_must: 0
          }];
          var ketiArr = [{
            id: 0,
            name: '请选择'
          }];
          var yanjiuArr =[{
            "id": 0,
            "name": "请选择"
          }];
          var tmparr = res.data.retData.CeShiXiangMu;
          for (var i = 0; i < tmparr.length; i++) {
            var obj = {};
            obj.id = tmparr[i].CeShiXiangMuID;
            obj.name = tmparr[i].CeShiXiangMuMingCheng;
            obj.sim_name = tmparr[i].CeShiXiangMuMingCheng.substring(0, 4);
            obj.ismust = tmparr[i].YangPinIsBiXu;
            ceshiArr.push(obj);
          }
          tmparr = res.data.retData.FuKuanKeTi;
          for (var i = 0; i < tmparr.length; i++) {
            var obj = {};
            obj.id = tmparr[i].KeTiZuID;
            obj.name = tmparr[i].KeTiZuMingCheng;
            ketiArr.push(obj);
          }
          tmparr = res.data.retData.KeYanFangXiang;
          for (var i = 0; i < tmparr.length; i++) {
            var obj = {};
            obj.id = tmparr[i].ZiDianZhiID;
            obj.name = tmparr[i].ZiDianZhiMingCheng;
            yanjiuArr.push(obj);
          }
          self.setData({
            ceshiArr: ceshiArr,
            ketiArr: ketiArr,
            yanjiuArr: yanjiuArr
          })
        }
      }
    });
  },

  addXm: function() {
    //获取测试项目ID
    var ceshiId = this.data.ceshiArr[this.data.ceshiIndex].id;
    if (ceshiId == 0) {
      wx.showToast({
        title: '请先选择检测项目',
        icon: 'none'
      })
      return;
    }
    var history_list = this.data.history_list;
    history_list = JSON.stringify(history_list);
    wx.navigateTo({
      url: "/pages/indexAll/selectXm/selectXm?history_list=" + history_list + "&ceshiId=" + ceshiId,
    })
  },
  removeXm: function(e) {
    var history_list = this.data.history_list;
    history_list.splice(e.currentTarget.dataset.index, 1);
    self.setData({
      history_list: history_list
    })
  },
  bindXmChange: function(e) {
    this.setData({
      xiangmuIndex: e.detail.value
    })
  },
  bindYjChange:function(e){
    this.setData({
      yanjiuIndex: e.detail.value
    })
  },
  telchange:function(e){
    this.setData({
      tel: e.detail.value
    })
  },
  bindKtChange: function(e) {
    this.setData({
      ketiIndex: e.detail.value
    });

    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=GetSuoShuXiangMuList",
      data: {
        KeTiZuID: self.data.ketiArr[self.data.ketiIndex].id,
        LoginID: loginid,
      },
      method: 'GET',
      success: function(res) {
        if (res.data.state == 1) {
          var xiangmuArr = [{
            id: 0,
            name: '请选择'
          }];
          var tmparr = res.data.retData.SuoShuXiangMu;
          for (var i = 0; i < tmparr.length; i++) {
            var obj = {};
            obj.id = tmparr[i].XiangMuID;
            obj.name = tmparr[i].XiangMuMingCheng;
            xiangmuArr.push(obj);
          }
          self.setData({
            xiangmuArr: xiangmuArr
          })
        }
      }
    });
  },
  bindCsChange: function(e) {
    this.setData({
      ceshiIndex: e.detail.value
    })
  },
  changeNum: function(e) {
    var history_list = this.data.history_list;
    for (let i = 0; i < history_list.length; i++) {
      history_list[i].num = e.detail.value;

    }
    self.setData({
      num: e.detail.value,
      history_list: history_list
    })
    
  },
  changeObjectNum: function(e) {
    var history_list = this.data.history_list;
    history_list[e.currentTarget.dataset.index].num = e.detail.value;
    self.setData({
      history_list: history_list
    })
  },
  changeObjectLaiyuan: function(e) {
    var history_list = this.data.history_list;
    history_list[e.currentTarget.dataset.index].laiyuan = e.detail.value;
    self.setData({
      history_list: history_list
    })
  },
  changeObjectBeizhu: function(e) {
    var history_list = this.data.history_list;
    history_list[e.currentTarget.dataset.index].beizhu = e.detail.value;
    self.setData({
      history_list: history_list
    })
  },
  bindStartDateChange: function(e) {
    this.setData({
      startdate: e.detail.value
    })
  },
  bindStartTimeChange: function(e) {
    this.setData({
      starttime: e.detail.value
    })
  },
  bindEndDateChange: function(e) {
    this.setData({
      enddate: e.detail.value
    })
  },
  bindEndTimeChange: function(e) {
    this.setData({
      endtime: e.detail.value
    })
  },
  goNext: function(e) {
   
    if (this.data.ceshiIndex == 0) {
      wx.showToast({
        title: '请选择检测项目',
        icon: 'none'
      });
      return false;
    }
    if(this.data.ketiIndex==0){
      wx.showToast({
        title: '请选择课题',
        icon:'none'
      });
      return false;
    }
    if (this.data.yanjiuIndex == 0) {
      wx.showToast({
        title: '请选择使用方向',
        icon: 'none'
      });
      return false;
    }
    if (this.data.tel == "") {
      wx.showToast({
        title: '请输入联系电话',
        icon: 'none'
      });
      return false;
    }
    var history = this.data.history_list;
    if(history.length<=0){
      wx.showToast({
        title: '至少添加一个样品',
        icon: 'none'
      });
      return false;
    }
    self.checkTime();
   
  },
  checkTime(){
    //开始组装并校验数据
    var KaiShiShiJian = this.data.startdate + " " + this.data.starttime;
    var JieShuShiJian = this.data.enddate + " " + this.data.endtime;
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=JudgeYuYueDate",
      data: {
        YiQiID: id,
        LoginID: loginid,
        KaiShiShiJian: KaiShiShiJian,
        JieShuShiJian: JieShuShiJian,
        YuYueMoShi: 0
      },
      method: 'GET',
      success: function (res) {
        if (res.data.state == 1) {                      //测试 为 0
           self.checkForm();
        } else if (res.data.state == -1) {
           app.goLogin();
        } else {
          wx.showToast({
            title: res.data.retMsg,
            icon: 'none'
          })
        }
      }
    });
  },
  checkForm(){
    //进入下一步，开始计算
    var JieYangShiJian = this.data.startdate + " " + this.data.starttime;
    var CurYangPingShu = 0;
    var history = this.data.history_list;
    for (var i = 0; i < history.length; i++) {
      CurYangPingShu += parseInt(history[i].num);
    }
    wx.request({
      url: app.globalData.apiUrl + "IS05/HIS050100.ashx?Method=JudgeYangPinShuLiang",
      data: {
        CeShiXiangMuId: self.data.ceshiArr[self.data.ceshiIndex].id,
        JieYangShiJian: JieYangShiJian,
        CurYangPingShu: CurYangPingShu,
        YiQiID: id,
        LoginID: loginid,
      },
      method: 'GET',
      success: function (res) {
        if (res.data.state == 1) {
          var obj = {};
          obj.mindate = self.data.startdate;
          obj.maxdate = self.data.enddate;
          obj.mintime = self.data.starttime;
          obj.maxtime = self.data.endtime;
          obj.keti = self.data.ketiArr[self.data.ketiIndex];
          obj.xiangmu = self.data.xiangmuArr[self.data.xiangmuIndex];
          obj.ceshi = self.data.ceshiArr[self.data.ceshiIndex];
          obj.history_list = self.data.history_list;
          obj.tel = self.data.tel;
          obj.yanjiu = self.data.yanjiuArr[self.data.yanjiuIndex];
          wx.navigateTo({
            url: '/pages/indexAll/yiqiStep3/yiqiStep3?tmpdata=' + JSON.stringify(obj) + "&id=" + id + "&loginid=" + loginid,
          })
        } else if (res.data.state == -1) {
          app.goLogin();
        } else {
          wx.showToast({
            title: res.data.retMsg,
            icon: 'none'
          })
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var history_list = this.data.history_list;
    for (let i = 0; i < history_list.length; i++) {
      history_list[i].num = this.data.num;
      history_list[i].laiyuan = "";
      history_list[i].beizhu = "";
    }
    console.log(history_list);
    self.setData({
      history_list: history_list
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})